home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / stuff / stuff.c < prev    next >
C/C++ Source or Header  |  1994-05-03  |  9KB  |  294 lines

  1. #include "stuff.h"
  2.  
  3. long prog_div;
  4. long uncomp_tot,comp_tot;
  5.  
  6. #ifndef min
  7. #define min(a,b) ((a)<(b)?(a):(b))
  8. #endif
  9.  
  10. struct sitHdr sh;
  11. struct fileHdr fh;
  12.  
  13. short ofd;
  14. ushort crc;
  15. int rmfiles;
  16. long total;
  17. char *Creator, *Type;
  18.  
  19. static unsigned short crctab[1<<8] = {
  20. 0x0, 0xc0c1, 0xc181, 0x140, 0xc301, 0x3c0, 0x280, 0xc241,
  21. 0xc601, 0x6c0, 0x780, 0xc741, 0x500, 0xc5c1, 0xc481, 0x440,
  22. 0xcc01, 0xcc0, 0xd80, 0xcd41, 0xf00, 0xcfc1, 0xce81, 0xe40,
  23. 0xa00, 0xcac1, 0xcb81, 0xb40, 0xc901, 0x9c0, 0x880, 0xc841,
  24. 0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
  25. 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
  26. 0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
  27. 0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
  28. 0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
  29. 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
  30. 0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
  31. 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
  32. 0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
  33. 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
  34. 0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
  35. 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
  36. 0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
  37. 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
  38. 0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
  39. 0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
  40. 0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
  41. 0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
  42. 0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
  43. 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
  44. 0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
  45. 0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
  46. 0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
  47. 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
  48. 0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
  49. 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
  50. 0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
  51. 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040,
  52. } ;
  53.  
  54. unsigned short updcrc(unsigned short icrc, unsigned char *icp, int icnt);
  55. void BeginProcessing(void);
  56. void EndProcessing(int numDocs);
  57. void ProcessDoc (FSSpec *curDocFSS, char mode);
  58. void scanfolder(FSSpec *);
  59.  
  60. unsigned short updcrc(unsigned short icrc, unsigned char *icp, int icnt)
  61.     {
  62.     register unsigned short crc = icrc;
  63.     register unsigned char *cp = icp;
  64.     register int cnt = icnt;
  65.     while( cnt-- ) 
  66.         {
  67.         crc = (crc>>8) ^ crctab[(crc & ((1<<8)-1)) ^ *cp++];
  68.         }
  69.     return( crc );
  70.     }
  71.  
  72. static cp2(
  73. unsigned short x,
  74. char dest[])
  75. {
  76.     dest[0] = x>>8;
  77.     dest[1] = x;
  78. }
  79.  
  80. static cp4(
  81. unsigned long x,
  82. char dest[])
  83. {
  84.     dest[0] = x>>24;
  85.     dest[1] = x>>16;
  86.     dest[2] = x>>8;
  87.     dest[3] = x;
  88. }
  89.  
  90. static FSSpec new;
  91. void    UpdateFileName(FSSpec *name);
  92.  
  93. extern long bytes_out;    /* Total number of byte to output */
  94.  
  95. void BeginProcessing(void)
  96.     {
  97.     uncomp_tot = 0;
  98.     comp_tot = 0;
  99.     total = 0;
  100.     }
  101.     
  102. void EndProcessing(int numDocs)
  103.     {
  104.     long inOutCount = 22;
  105.     ChkOsErr(SetFPos(ofd,1,0));
  106.     total += inOutCount;
  107.     UpdateFileName(&new);
  108.     /* header header */
  109.     memcpy(sh.sig1,"SIT!",4);
  110.     cp2(numDocs,(char *)sh.numFiles);
  111.     cp4(total,(char *)sh.arcLen);
  112.     memcpy(sh.sig2,"rLau",4);
  113.     sh.version = 1;
  114.     ChkOsErr(FSWrite(ofd,&inOutCount,&sh));
  115.     FSClose(ofd);
  116.     ofd = 0;
  117.     }
  118.  
  119. void ProcessDoc (FSSpec *curDocFSS, char mode)
  120.     {
  121.     long inOutCount;
  122.     OSErr err,iErr;
  123.     int siz;
  124.     int i,n;
  125.     short fd;
  126.     long fpos1, fpos2;
  127.     if (!total)
  128.         {
  129.         new = *curDocFSS;
  130.         if (new.name[0] < 28)
  131.             {
  132.             BlockMove(".sit", &new.name[new.name[0]+1], 4);
  133.             new.name[0] += 4;
  134.             }
  135.         else
  136.             BlockMove("\parchive.sit", &new.name, 12);
  137.         iErr = FSpCreate(&new,'SIT!','SIT!',0);
  138.         if (iErr != dupFNErr) ChkOsErr(iErr);
  139.         ChkOsErr(FSpOpenDF(&new,0,&ofd));    
  140.         ChkOsErr(SetEOF(ofd,22));
  141.         ChkOsErr(SetFPos(ofd,1,22));
  142.         }
  143.     else
  144.         {
  145.         if (!memcmp(&new, curDocFSS, sizeof(FSSpec))) return;
  146.         }
  147.     UpdateFileName(curDocFSS);
  148.     ChkOsErr(GetFPos(ofd,&fpos1));
  149.     /* write empty header, will seek back and fill in later */
  150.     inOutCount = 112;
  151.     memset(&fh,0,inOutCount);
  152.     memcpy((void *)fh.fName, &(curDocFSS->name[0]), 1+curDocFSS->name[0]);
  153.     if (mode)    /* operate on directory entry */
  154.         {
  155.         CInfoPBRec curDocPB;
  156.         curDocPB.hFileInfo.ioVRefNum = curDocFSS->vRefNum;
  157.         curDocPB.hFileInfo.ioDirID = curDocFSS->parID;
  158.         curDocPB.hFileInfo.ioNamePtr = curDocFSS->name;
  159.         curDocPB.hFileInfo.ioFDirIndex = 0;    
  160.         err = PBGetCatInfoSync (&curDocPB);
  161.         memcpy((void *)fh.cDate, (void *)&curDocPB.hFileInfo.ioFlCrDat, 4);
  162.         memcpy((void *)fh.mDate, (void *)&curDocPB.hFileInfo.ioFlMdDat, 4);
  163.         fh.compRMethod = fh.compDMethod = mode;
  164.         cp2(updcrc(0,(unsigned char *)&fh,110), (char *)fh.hdrCRC);
  165.         ChkOsErr(FSWrite(ofd,&inOutCount,&fh));
  166.         total += inOutCount;
  167.         return;
  168.         }
  169.     else
  170.         {
  171.         HParamBlockRec pb;
  172.         ChkOsErr(FSWrite(ofd,&inOutCount,&fh));
  173.         memset(&pb, 0, sizeof(HParamBlockRec));
  174.         pb.fileParam.ioVRefNum = curDocFSS->vRefNum;
  175.         pb.fileParam.ioDirID = curDocFSS->parID;
  176.         pb.fileParam.ioNamePtr = curDocFSS->name;
  177.         err = PBHGetFInfoSync (&pb);
  178.         memcpy((void *)fh.fType, (void *)&pb.fileParam.ioFlFndrInfo.fdType, 4);
  179.         memcpy((void *)fh.fCreator, (void *)&pb.fileParam.ioFlFndrInfo.fdCreator, 4);
  180.         memcpy((void *)fh.FndrFlags, (void *)&pb.fileParam.ioFlFndrInfo.fdFlags, 2);
  181.         memcpy((void *)fh.cDate, (void *)&pb.fileParam.ioFlCrDat, 4);
  182.         memcpy((void *)fh.mDate, (void *)&pb.fileParam.ioFlMdDat, 4);
  183.         cp4(pb.fileParam.ioFlRLgLen,(char *)fh.rLen);
  184.         cp4(pb.fileParam.ioFlLgLen,(char *)fh.dLen);
  185.         /* look for resource fork */
  186.         if (pb.fileParam.ioFlRLgLen) 
  187.             {    /* resource fork exists */
  188.             long fpos3;
  189.             OSErr err = FSpOpenRF(curDocFSS, fsRdPerm, &fd);
  190.             crc = 0;
  191.             prog_div = 0x4000000L/pb.fileParam.ioFlRLgLen;
  192.             ChkOsErr(GetFPos(ofd,&fpos3));
  193.             compress(fd, ofd);
  194.             if (bytes_out > pb.fileParam.ioFlRLgLen)
  195.                 {
  196.                 ChkOsErr(SetFPos(fd,1,0));
  197.                 ChkOsErr(SetEOF(ofd,fpos3));
  198.                 copy(fd, ofd);
  199.                 bytes_out = pb.fileParam.ioFlRLgLen;
  200.                 }
  201.             else fh.compRMethod = lpzComp;
  202.             FSClose(fd);
  203.             fd = 0;
  204.             uncomp_tot += pb.fileParam.ioFlRLgLen;
  205.             comp_tot += bytes_out;
  206.             cp4(bytes_out,(char *)fh.cRLen);
  207.             cp2(crc,(char *)fh.rsrcCRC);
  208.             }
  209.         /* look for data fork */
  210.         if (pb.fileParam.ioFlLgLen) 
  211.             {    /* data fork exists */
  212.             long fpos3;
  213.             OSErr err = FSpOpenDF(curDocFSS, fsRdPerm, &fd);
  214.             crc = 0;
  215.             prog_div = 0x4000000L/pb.fileParam.ioFlLgLen;
  216.             ChkOsErr(GetFPos(ofd,&fpos3));
  217.             compress(fd, ofd);
  218.             if (bytes_out > pb.fileParam.ioFlLgLen)
  219.                 {
  220.                 ChkOsErr(SetFPos(fd,1,0));
  221.                 ChkOsErr(SetEOF(ofd,fpos3));
  222.                 copy(fd, ofd);
  223.                 bytes_out = pb.fileParam.ioFlLgLen;
  224.                 }
  225.             else fh.compDMethod = lpzComp;
  226.             FSClose(fd);
  227.             fd = 0;
  228.             uncomp_tot += pb.fileParam.ioFlLgLen;
  229.             comp_tot += bytes_out;
  230.             cp4(bytes_out,(char *)fh.cDLen);
  231.             cp2(crc,(char *)fh.dataCRC);
  232.             }
  233.         cp2(updcrc(0,(unsigned char *)&fh,110), (char *)fh.hdrCRC);
  234.         ChkOsErr(GetFPos(ofd,&fpos2));
  235.         ChkOsErr(SetFPos(ofd,1,fpos1));
  236.         inOutCount = 112;
  237.         ChkOsErr(FSWrite(ofd,&inOutCount,&fh));
  238.         ChkOsErr(SetFPos(ofd,1,fpos2));
  239.         total += (fpos2 - fpos1);
  240.         }
  241.     }
  242.  
  243. void scanfolder(FSSpec *curDocFSS)
  244.     {
  245.     CInfoPBRec curDocPB;
  246.     long fpos1,fpos2,inOutCount;
  247.     long old_uncomp_tot = uncomp_tot;
  248.     long old_comp_tot = comp_tot;
  249.     int i;
  250.     FSSpec    new;
  251.     OSErr err;
  252.     OSErr iErr;
  253.     ProcessDoc(curDocFSS, 0x20);
  254.     ChkOsErr(GetFPos(ofd,&fpos1));
  255.     curDocPB.hFileInfo.ioVRefNum = curDocFSS->vRefNum;
  256.     curDocPB.hFileInfo.ioDirID = curDocFSS->parID;
  257.     curDocPB.hFileInfo.ioNamePtr = curDocFSS->name;
  258.     curDocPB.hFileInfo.ioFDirIndex = 0;    
  259.     err = PBGetCatInfoSync (&curDocPB);
  260.     new.parID = curDocPB.hFileInfo.ioDirID;
  261.     new.vRefNum = curDocFSS->vRefNum;
  262.     for (i = 1; err != fnfErr; i++) 
  263.         {
  264.         curDocPB.hFileInfo.ioVRefNum = new.vRefNum;
  265.         curDocPB.hFileInfo.ioDirID = new.parID;
  266.         curDocPB.hFileInfo.ioNamePtr = new.name;
  267.         curDocPB.hFileInfo.ioFDirIndex = i;    
  268.         err = PBGetCatInfoSync (&curDocPB);
  269.         if (err == noErr)
  270.             {
  271.             Boolean isFolder,wasAlias;
  272.             err = ResolveAliasFile (&new, true, &isFolder, &wasAlias);
  273.             if (err == noErr) 
  274.                 {
  275.                 if (isFolder) scanfolder(&new);
  276.                 else ProcessDoc (&new, 0x0);
  277.                 }
  278.             }
  279.         }
  280.     ProcessDoc(curDocFSS, 0x21);
  281.     ChkOsErr(GetFPos(ofd,&fpos2));
  282.     inOutCount = 112;
  283.     fpos1 -= inOutCount;
  284.     ChkOsErr(SetFPos(ofd,1,fpos1));
  285.     ChkOsErr(FSRead(ofd,&inOutCount,&fh));
  286.     inOutCount = 112;
  287.     cp4(fpos2-fpos1-inOutCount,(char *)fh.cDLen);
  288.     cp4(uncomp_tot-old_uncomp_tot,(char *)fh.dLen);
  289.     cp2(updcrc(0,(unsigned char *)&fh,110), (char *)fh.hdrCRC);
  290.     ChkOsErr(SetFPos(ofd,1,fpos1));
  291.     ChkOsErr(FSWrite(ofd,&inOutCount,&fh));
  292.     ChkOsErr(SetFPos(ofd,1,fpos2));
  293.     }
  294.